package com.xing.binbackend.pattern.factory12.operation.lazyinit;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

// 自定义条件判断类
public class WechatEnabledCondition implements Condition {

    /**
     * 核心判断逻辑：
     * 当配置文件中 payment.wechat.enabled=true 时生效
     * # 控制微信支付的开关
     * payment.wechat.enabled=true
     * payment.wechat.mchid=1234567890
     * payment.wechat.key=your_api_key_here
     */
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return context.getEnvironment().getProperty("payment.wechat.enabled", Boolean.class, false);
    }

    // 增强版条件判断（同时检查环境变量）
    public boolean matches2(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Environment env = context.getEnvironment();

        return env.getProperty("payment.wechat.enabled", Boolean.class, false)
                && !env.containsProperty("DISABLE_WECHAT_PAYMENT"); // 检查环境变量
    }
}

